package com.sudo.service.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.sudo.common.constant.DictConstant;
import com.sudo.common.utils.LocalDateTimeUtil;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.dao.admin.entity.SysDict;
import com.sudo.dao.admin.mapper.SysDictMapper;
import com.sudo.dao.admin.model.bo.SysDictBO;
import com.sudo.service.admin.service.AdminBaseService;
import com.sudo.service.admin.service.SysDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 为道日损 2020-06-23 16:49
 * webSite：https://www.sudo.ren
 */
@Service
public class SysDictServiceImpl extends AdminBaseService implements SysDictService {

    @Resource
    private SysDictMapper sysDictMapper;

    @Transactional(propagation = Propagation.REQUIRED ,rollbackFor = Exception.class)
    @Override
    public int addDict(SysDictBO sysDictBO, String sessionUser) {
        SysDict sysDict = new SysDict();
        BeanUtils.copyProperties(sysDictBO,sysDict);
        if (StringUtils.isBlank(sysDictBO.getpCode())) {
            sysDict.setpCode(DictConstant.DICT_P_CODE);
        }
        String now = LocalDateTimeUtil.getStandardDateTimeStr();
        sysDict.setCreateBy(sessionUser);
        sysDict.setCreateTime(now);
        return sysDictMapper.insertSelective(sysDict);
    }

    @Transactional(propagation = Propagation.SUPPORTS ,rollbackFor = Exception.class)
    @Override
    public SysDict getSysDictByCode(String dictCode) {
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("dictCode", dictCode);
        return sysDictMapper.selectOneByExample(example);
    }

    @Transactional(propagation = Propagation.SUPPORTS ,rollbackFor = Exception.class)
    @Override
    public PagedGridResult getParentDictByPage(int page , int pageSize) {
        PageHelper.startPage(page, pageSize);
        Example example = new Example(SysDict.class);
        example.orderBy("id").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pCode", DictConstant.DICT_P_CODE);
        List<SysDict> dictList = sysDictMapper.selectByExample(example);
        return setterPagedGrid(dictList ,page);
    }

    @Transactional(propagation = Propagation.SUPPORTS ,rollbackFor = Exception.class)
    @Override
    public List<SysDict> getChildDictByPCode(String pCode) {
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pCode", pCode);
        return sysDictMapper.selectByExample(example);
    }

    @Transactional(propagation = Propagation.REQUIRED ,rollbackFor = Exception.class)
    @Override
    public int updateDictByDictCode(SysDictBO sysDictBO,String sessionUser) {
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("dictCode", sysDictBO.getDictCode());
        String now = LocalDateTimeUtil.getStandardDateTimeStr();
        SysDict sysDict = new SysDict();
        sysDict.setUpdateBy(sessionUser);
        sysDict.setUpdateTime(now);
        BeanUtils.copyProperties(sysDictBO,sysDict);
        return sysDictMapper.updateByExampleSelective(sysDict,example);
    }

    @Transactional(propagation = Propagation.REQUIRED ,rollbackFor = Exception.class)
    @Override
    public int deleteDictByCode(String dictCode) {
        //删除子级字典
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pCode", dictCode);
        sysDictMapper.deleteByExample(example);

        SysDict sysDict = new SysDict();
        sysDict.setDictCode(dictCode);
        return sysDictMapper.delete(sysDict);
    }


}
